/*
 * Copyright 2010 Gaurav Saxena
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.gwtstructs.gwt.client.widgets.autocompleterTextbox;

import java.util.List;

import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class DelimeterEnabledCustomSuggestionAutoCompleterTextBox extends CustomSuggestionAutoCompleterTextBox{
	private String delimiter;
	/**
	 * @param suggestions list of objects of {@link ICustomSuggestion} 
	 * @param position Position of Suggestion Panel among left, top, right, bottom
	 */
	public DelimeterEnabledCustomSuggestionAutoCompleterTextBox(List<ICustomSuggestion> suggestions
			, PanelPosition position, String delimiter) {
		super(suggestions, position);
		this.delimiter = delimiter;
	}
	/**
	 * @param suggestions list of objects of {@link ICustomSuggestion} 
	 * @param isComparisonCaseSensitive if the String comparison needs to be case sensitive
	 * @param position Position of Suggestion Panel among left, top, right, bottom
	 */
	public DelimeterEnabledCustomSuggestionAutoCompleterTextBox(List<ICustomSuggestion> suggestions, boolean isComparisonCaseSensitive
			, PanelPosition position, String delimiter) {
		super(suggestions, isComparisonCaseSensitive, position);
		this.delimiter = delimiter;
	}
	/**
	 * @param suggestions list of objects of {@link ICustomSuggestion}
	 * @param isComparisonCaseSensitive if the String comparison needs to be case sensitive. Default: false
	 * @param isComparisonStartsFromBeginning if the comparison should begin from the beginning of the string. 
	 * Default: false
	 * @param position Position of Suggestion Panel among left, top, right, bottom
	 */
	public DelimeterEnabledCustomSuggestionAutoCompleterTextBox(List<ICustomSuggestion> suggestions, boolean isComparisonCaseSensitive,
			boolean isComparisonStartsFromBeginning, PanelPosition position, String delimiter) {
		super(suggestions, isComparisonCaseSensitive, isComparisonStartsFromBeginning, position);
		this.delimiter = delimiter;
	}
	/**
	 * @param suggestions list of objects of {@link ICustomSuggestion} 
	 * @param delimiter string after which the suggestions will show up again e.g. ",",":" etc 
	 */
	public DelimeterEnabledCustomSuggestionAutoCompleterTextBox(List<ICustomSuggestion> suggestions
			, String delimiter) {
		super(suggestions);
		this.delimiter = delimiter;
	}
	/**
	 * delimiter is assumed to be ","
	 * @param suggestions list of objects of {@link ICustomSuggestion} 
	 */
	public DelimeterEnabledCustomSuggestionAutoCompleterTextBox(List<ICustomSuggestion> suggestions) {
		super(suggestions);
		this.delimiter = ",";
	}
	protected DelimeterEnabledCustomSuggestionAutoCompleterTextBox(TextBox textBox, final List<String> suggestions, PanelPosition position){
		this(textBox, textBox, suggestions, position);
	}
	protected DelimeterEnabledCustomSuggestionAutoCompleterTextBox(final TextBox textBox, Widget textboxContainer, final List<String> suggestions
			, PanelPosition position){
		super(textBox, textboxContainer, suggestions, position);
	}
	@Override
	protected String getCurrentText() {
		String[] tokens = getText().split(delimiter);
		if(getText().lastIndexOf(delimiter) == getText().length() - 1)
			return "";
		else
			return tokens[tokens.length - 1].trim();
	}
	@Override
	protected void setTextBoxValue(List<String> suggestions, int suggestionIndex) {
		int commaIndex = getText().lastIndexOf(delimiter);
		if(commaIndex == -1)
			setText(suggestions.get(suggestionIndex));
		else
			setText(getText().substring(0, commaIndex + 1) + suggestions.get(suggestionIndex));
	}
}
